/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.supplier.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import queenb.net.entitybean.K100;
import queenb.net.entitybean.N100;
import queenb.web.controller.QueenbController;
import queenb.web.crm.store.CustomerStore;
import queenb.web.crm.store.K100Store;
import queenb.web.util.ComboboxUtil;
import queenb.web.util.ContextUtil;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author VANANH
 */
public class K100supplierController extends QueenbController {

    /**
     * @return the numrow
     */
    public static int getNumrow() {
        if (numrow == 0) {
            numrow = 30;
        }
        return numrow;
    }

    /**
     * @param aNumrow the numrow to set
     */
    public static void setNumrow(int aNumrow) {
        numrow = aNumrow;
    }
    private K100Store k100Store;
    private String KV104;// Nguoi gioi thieu 
    private String KV102;// 
    private String KV101;
    private String KV103;
    private String KV105;// Mã nhà cung cấp
    private int PK100;
    private int FN550;
    private int FN750;
    private int FN850;
    private int FN900;
    private List<K100> list = new ArrayList<K100>();
    private List<SelectItem> combN550 = new ArrayList<SelectItem>();
    private List<SelectItem> selectItemN900 = new ArrayList<SelectItem>();
    private List<SelectItem> selectCombN850 = new ArrayList<SelectItem>();
    ComboboxUtil comboboxUtil = new ComboboxUtil();
    private CustomerStore customerStore;
    private String idSearch;
    private String keySearch = getResourceString("addkey");
    private int typeSearch = 0;
    private int fk400;
    private String loginEmp;
    private String userAction;
    private int dayss;
    private Date KD115;
    private int Offset = 0;
    private int Limit = 30; // tong so hien thi 
    private int totalrow; // tong so dong
    private int totalpage; // tong so trang
    private int current; // traung hien tai
    static private int numrow = 30; // so dong tren 1 trang
    private int start; // bat dau lay tu record
    private int end; // lay tu record so
    private int startpage = 1; // page bat dau
    private int endpage = 1;   // page ket thuc
    private String pagename = "";
    private int totalcurent;
    private int typesearchcurent;
    private String Emplogin;
    private String tabString;

    public K100supplierController() {
        if (SessionProvider.getSessionMapValue("emp") != null) {
            loginEmp = SessionProvider.getSessionMapValue("emp").toString();
            userAction = loginEmp;
        }

        k100Store = new K100Store();
        searchAlpha();
        getKapiMenu().setAdmin(false);
        getKapiMenu().setMainmenu1("m124");
        Object fk100 = getRequest().getParameter("pnPK100");
        if (fk100 != null) {
            this.PK100 = Util.parseInt(fk100);
            list = k100Store.getListTabK100WithID(getLogin().getUserName(), PK100);

            //getList();
            if (list != null && list.size() > 0) {

                KV101 = list.get(0).getKv101();
                KV103 = list.get(0).getKv103();
                KV102 = list.get(0).getKv102();
                //  KN114 = list.get(0).getKn114();
                KV104 = list.get(0).getKv104();
                KV105 = list.get(0).getKv105();
                //  KV110 = list.get(0).getKv110();
                FN850 = list.get(0).getFn850();
                FN550 = list.get(0).getFn550();
            }
        }
        
        Object objAction = getRequest().getParameter("action");
        if(objAction!=null){
            if(objAction.equals("getitemvaluePk100")){
                Object Objectidk100 = getRequest().getParameter("pk100");
                if (Objectidk100 != null) {
                    PK100 = Util.parseInt(Objectidk100);
                    System.out.println("----------------------ID CUA SUPPLIER WHEN CLICK VAO-------------" + PK100);
                    SessionProvider.setSessionMapValue("PN100HD", PK100);
                }
            }
        }
    }

    public void reload() {
        try {

            SessionProvider.setSessionMapValue("typeSearchK100Su", typeSearch);

            SessionProvider.setSessionMapValue("keySearchK100Su", keySearch);
        } catch (Exception e) {
        }
    }

    public void searchAlpha() {
        try {
            setIdSearch(ContextUtil.getRequestParameter("idSearch"));
            if (getIdSearch() != null && getIdSearch().contains("ALL")) {
                setIdSearch(null);
            }
            getLogin().setCharacter(getIdSearch());
        } catch (Exception ex) {
            setIdSearch("ALL");
        }

    }

    public List<SelectItem> getListSelectItems() {
        customerStore = new CustomerStore();
        List<N100> listN100Select = customerStore.getSelectItemN550(getLogin().getModule(), 99, 0, getLogin().getUserName());
        Iterator ic = listN100Select.iterator();
        List<SelectItem> listSelected = new ArrayList<SelectItem>();
        while (ic.hasNext()) {
            N100 n100 = (N100) ic.next();
            SelectItem item = new SelectItem(n100.getVal(), n100.getLabel());

            listSelected.add(item);
        }
        return listSelected;
    }

    //tim theo quoc gia va nganhnghe
    public void reloadlistCC() {
        try {
            System.out.println("ActionEvent----------------------------------------" + loginEmp + FN750 + FN550);
            userAction = loginEmp;
            //   setDayss(0);
            setUserAction(getLoginEmp());
            SessionProvider.setSessionMapValue("typeSearchK100", 0);
            SessionProvider.setSessionMapValue("keySearchK100", null);
            SessionProvider.setSessionMapValue("TYTYPE", 10);
            SessionProvider.setSessionMapValue("TYPEFN750CC", FN750);
            SessionProvider.setSessionMapValue("TYPEFN550CC", FN550);
            SessionProvider.setSessionMapValue("emp", loginEmp);

        } catch (Exception e) {
        }

    }

    public void reloadlist(ActionEvent ac) {
        System.out.println("ActionEvent----------------------------------------" + loginEmp);
        userAction = loginEmp;
        //   setDayss(0);
        setUserAction(getLoginEmp());
        if (SessionProvider.getSessionMapValue("TYTYPE") != null) {
            SessionProvider.removeSessionMapValue("TYTYPE");
        }
        SessionProvider.setSessionMapValue("typeSearchK100", 0);
        SessionProvider.setSessionMapValue("keySearchK100", null);
        SessionProvider.setSessionMapValue("emp", loginEmp);
        sendRedirect("supplier.jsp");
    }

    public List<K100> getListK100supplierAll() {
        try {
            List<K100> listK100supplier = new ArrayList<K100>();
            //   SessionProvider.setSessionMapValue("typeSearchK100", typeSearch);
            if (SessionProvider.getSessionMapValue("TYTYPE") != null) {
                System.out.println("-----------------COUNTRY--------------" + FN750 + "-----------------CAREEW--------------" + FN550);
                listK100supplier = k100Store.LISTOFTABK100CCA(getLogin().getModule(), getLogin().getCompanyNo(), PK100, 2, 0, FN550, FN750, Offset, Limit, 0, 0, getLogin().getUserName());
            } else {
                if (SessionProvider.getSessionMapValue("keySearchK100") != null) {
                    keySearch = SessionProvider.getSessionMapValue("keySearchK100").toString();
                }
                if (SessionProvider.getSessionMapValue("typeSearchK100") != null) {
                    typeSearch = Util.parseInt(SessionProvider.getSessionMapValue("typeSearchK100"));
                }
                System.out.println("===============THONG TIN--------------" + keySearch + typeSearch);


                if (typeSearch == 1) {
                    listK100supplier = k100Store.getListCompany_NA(getLogin().getModule(), getLogin().getCompanyNo(), keySearch, 0, 50, 0, 0, getLogin().getUserName());
                } else {
                    System.out.println("=======TUng========" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + PK100 + "," + 2 + "," + fk400 + "," + 1 + "," + null + "," + 0 + "," + 30 + "," + 0 + "," + 0 + "," + userAction);
                    listK100supplier = k100Store.getListCompany_ALL(getLogin().getModule(), getLogin().getCompanyNo(), PK100, 2, fk400, 1, null, 0, 30, 0, 0, userAction);

                }



            }
            K100 n100;
            if (Offset == 0) {
                if (listK100supplier.size() > 0) {

                    n100 = listK100supplier.get(0);
                    if (n100 != null) {
                        SessionProvider.setSessionMapValue("totalcurentK100CC", n100.getRowss());
                        //totalcurent = n100.getRowss();
                    } else {
                        SessionProvider.setSessionMapValue("totalcurentK100CC", 0);
                        //totalcurent = 0;
                    }
                } else {
                    SessionProvider.setSessionMapValue("totalcurentK100CC", 0);
                    //totalcurent = 0;
                }
            }

            int t;
            Object obj = SessionProvider.getSessionMapValue("totalcurentK100CC");
            if (obj != null) {
                try {
                    t = Integer.parseInt(obj.toString());
                } catch (Exception e) {
                    t = 0;
                }
                totalcurent = t;
            } else {
                totalcurent = 0;
            }

            setTotalrow(totalcurent);
            return listK100supplier;
        } catch (Exception ex) {
            System.out.println("===============" + ex.getMessage());
            return null;
        }
    }

    public void saveK100() {
        try {
            System.out.println("--------------k100controllersave" + KV103 + KV104 + KV101);
            K100Store store = new K100Store();
            int roweffect = 0;
            if (KV104 == null || KV104.equals("0")) {
                KV104 = getLogin().getUserName();
                System.out.println("--------------k100controllersave" + KV103 + KV104 + KV101);
            }
            if (KV102 == null || KV102.equals("0")) {
                KV102 = getLogin().getUserName();
            }
            //roweffect = store.updateK100By(0, 7, 1, 99, 1, 0, 0, 0, 0, null, null, null, null, null, null, null, null, null, null, null, null, 0, 0, "admin@sgt.com")
            roweffect = store.updateK100By(
                    this.PK100, getLogin().getModule(), 0, getLogin().getCompanyNo(), 1, "CC", 0, 0,
                    2, 0, this.KV101, this.KV102, this.KV103, this.KV104, this.KV105, null, null, null,
                    null, null, null, null, 0, Util.toSQLDate(getKD115()), 0, 0, this.FN550, getLogin().getUserName());
            if (roweffect > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            }
            sendRedirect("supplier.jsp");
        } catch (Exception e) {
            System.out.println("-------------------------------" + e.getMessage());
        }

    }

    public void changeCombN850forN900(ActionEvent ac) {
        selectItemN900 = comboboxUtil.getSelectedN900Comb(FN850);
        System.out.println("---change" + FN850 + "--" + selectItemN900.size());
    }

    /**
     * @return the KV104
     */
    public String getKV104() {
        return KV104;
    }

    /**
     * @param KV104 the KV104 to set
     */
    public void setKV104(String KV104) {
        this.KV104 = KV104;
    }

    /**
     * @return the KV102
     */
    public String getKV102() {
        return KV102;
    }

    /**
     * @param KV102 the KV102 to set
     */
    public void setKV102(String KV102) {
        this.KV102 = KV102;
    }

    /**
     * @return the KV101
     */
    public String getKV101() {
        return KV101;
    }

    /**
     * @param KV101 the KV101 to set
     */
    public void setKV101(String KV101) {
        this.KV101 = KV101;
    }

    /**
     * @return the KV103
     */
    public String getKV103() {
        return KV103;
    }

    /**
     * @param KV103 the KV103 to set
     */
    public void setKV103(String KV103) {
        this.KV103 = KV103;
    }

    /**
     * @return the PK100
     */
    public int getPK100() {
        return PK100;
    }

    /**
     * @param PK100 the PK100 to set
     */
    public void setPK100(int PK100) {
        this.PK100 = PK100;
    }

    /**
     * @return the FN550
     */
    public int getFN550() {
        return FN550;
    }

    /**
     * @param FN550 the FN550 to set
     */
    public void setFN550(int FN550) {
        this.FN550 = FN550;
    }

    /**
     * @return the combN550
     */
    public List<SelectItem> getCombN550() {
        combN550 = comboboxUtil.getSelectedN550Comb();
        return combN550;
    }

    /**
     * @param combN550 the combN550 to set
     */
    public void setCombN550(List<SelectItem> combN550) {
        this.combN550 = combN550;
    }

    /**
     * @return the FN850
     */
    public int getFN850() {
        return FN850;
    }

    /**
     * @param FN850 the FN850 to set
     */
    public void setFN850(int FN850) {
        this.FN850 = FN850;
    }

    /**
     * @return the FN900
     */
    public int getFN900() {
        return FN900;
    }

    /**
     * @param FN900 the FN900 to set
     */
    public void setFN900(int FN900) {
        this.FN900 = FN900;
    }

    /**
     * @return the selectItemN900
     */
    public List<SelectItem> getSelectItemN900() {
        return selectItemN900;
    }

    /**
     * @param selectItemN900 the selectItemN900 to set
     */
    public void setSelectItemN900(List<SelectItem> selectItemN900) {
        this.selectItemN900 = selectItemN900;
    }

    /**
     * @return the selectCombN850
     */
    public List<SelectItem> getSelectCombN850() {
        selectCombN850 = comboboxUtil.getSelectedN850Comb();
        return selectCombN850;
    }

    /**
     * @param selectCombN850 the selectCombN850 to set
     */
    public void setSelectCombN850(List<SelectItem> selectCombN850) {
        this.selectCombN850 = selectCombN850;
    }

    /**
     * @return the KV105
     */
    public String getKV105() {
        return KV105;
    }

    /**
     * @param KV105 the KV105 to set
     */
    public void setKV105(String KV105) {
        this.KV105 = KV105;
    }

    /**
     * @return the idSearch
     */
    public String getIdSearch() {
        return idSearch;
    }

    /**
     * @param idSearch the idSearch to set
     */
    public void setIdSearch(String idSearch) {
        this.idSearch = idSearch;
    }

    /**
     * @return the keySearch
     */
    public String getKeySearch() {
        return keySearch;
    }

    /**
     * @param keySearch the keySearch to set
     */
    public void setKeySearch(String keySearch) {
        this.keySearch = keySearch;
    }

    /**
     * @return the typeSearch
     */
    public int getTypeSearch() {
        if (SessionProvider.getSessionMapValue("typeSearchK100") != null) {
            typeSearch = Util.parseInt(SessionProvider.getSessionMapValue("typeSearchK100"));
            System.out.println("---------id tìm kieemmm--------------------" + typeSearch);
        }
        return typeSearch;
    }

    /**
     * @param typeSearch the typeSearch to set
     */
    public void setTypeSearch(int typeSearch) {
        this.typeSearch = typeSearch;
    }

    /**
     * @return the fk400
     */
    public int getFk400() {
        return fk400;
    }

    /**
     * @param fk400 the fk400 to set
     */
    public void setFk400(int fk400) {
        this.fk400 = fk400;
    }

    /**
     * @return the loginEmp
     */
    public String getLoginEmp() {
        return loginEmp;
    }

    /**
     * @param loginEmp the loginEmp to set
     */
    public void setLoginEmp(String loginEmp) {
        this.loginEmp = loginEmp;
    }

    /**
     * @return the userAction
     */
    public String getUserAction() {
        return userAction;
    }

    /**
     * @param userAction the userAction to set
     */
    public void setUserAction(String userAction) {
        this.userAction = userAction;
    }

    /**
     * @return the Emplogin
     */
    public String getEmplogin() {
        return Emplogin;
    }

    /**
     * @param Emplogin the Emplogin to set
     */
    public void setEmplogin(String Emplogin) {
        this.Emplogin = Emplogin;
    }

    /**
     * @return the FN750
     */
    public int getFN750() {
        return FN750;
    }

    /**
     * @param FN750 the FN750 to set
     */
    public void setFN750(int FN750) {
        this.FN750 = FN750;
    }

    /**
     * @return the dayss
     */
    public int getDayss() {
        return dayss;
    }

    /**
     * @param dayss the dayss to set
     */
    public void setDayss(int dayss) {
        this.dayss = dayss;
    }

    /**
     * @return the KD115
     */
    public Date getKD115() {
        return KD115;
    }

    /**
     * @param KD115 the KD115 to set
     */
    public void setKD115(Date KD115) {
        this.KD115 = KD115;
    }

    /**
     * @return the Offset
     */
    public int getOffset() {
        return Offset;
    }

    /**
     * @param Offset the Offset to set
     */
    public void setOffset(int Offset) {
        this.Offset = Offset;
    }

    /**
     * @return the Limit
     */
    public int getLimit() {
        return Limit;
    }

    /**
     * @param Limit the Limit to set
     */
    public void setLimit(int Limit) {
        this.Limit = Limit;
    }

    /**
     * @return the totalrow
     */
    public int getTotalrow() {
        return totalrow;
    }

    /**
     * @param totalrow the totalrow to set
     */
    public void setTotalrow(int totalrow) {
        totalpage = totalrow / numrow;

        if (totalrow % numrow > 0) {
            totalpage = totalpage + 1;
        }


        if (getTotalpage() <= 10) {
            setStartpage(1);
            setEndpage(getTotalpage());

        } else {
            setStartpage(current - 5);
            setEndpage(current + 5);


        }
        this.totalrow = totalrow;
    }

    /**
     * @return the totalpage
     */
    public int getTotalpage() {
        return totalpage;
    }

    /**
     * @param totalpage the totalpage to set
     */
    public void setTotalpage(int totalpage) {
        this.totalpage = totalpage;
    }

    /**
     * @return the current
     */
    public int getCurrent() {
        if (current > totalpage) {
            current = totalpage;
        } else if (current < 1) {
            current = 1;
        }
        return current;
    }

    /**
     * @param current the current to set
     */
    public void setCurrent(int current) {
        start = (current - 1) * getNumrow();
        end = current * numrow;
        this.current = current;
    }

    /**
     * @return the start
     */
    public int getStart() {
        if (start == 0) {
            start = 1;
        }
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(int start) {
        this.start = start;
    }

    /**
     * @return the end
     */
    public int getEnd() {
        if (end == 0) {
            end = 20;
        }
        return end;
    }

    /**
     * @param end the end to set
     */
    public void setEnd(int end) {
        this.end = end;
    }

    /**
     * @return the startpage
     */
    public int getStartpage() {
        if (startpage < 1) {
            startpage = 1;
        }
        return startpage;
    }

    /**
     * @param startpage the startpage to set
     */
    public void setStartpage(int startpage) {
        this.startpage = startpage;
    }

    /**
     * @return the endpage
     */
    public int getEndpage() {
        if (endpage > totalpage) {
            endpage = totalpage;
        }
        return endpage;
    }

    /**
     * @param endpage the endpage to set
     */
    public void setEndpage(int endpage) {
        this.endpage = endpage;
    }

    /**
     * @return the pagename
     */
    public String getPagename() {
        return pagename;
    }

    /**
     * @param pagename the pagename to set
     */
    public void setPagename(String pagename) {
        this.pagename = pagename;
    }

    /**
     * @return the totalcurent
     */
    public int getTotalcurent() {
        return totalcurent;
    }

    /**
     * @param totalcurent the totalcurent to set
     */
    public void setTotalcurent(int totalcurent) {
        this.totalcurent = totalcurent;
    }

    /**
     * @return the typesearchcurent
     */
    public int getTypesearchcurent() {
        return typesearchcurent;
    }

    /**
     * @param typesearchcurent the typesearchcurent to set
     */
    public void setTypesearchcurent(int typesearchcurent) {
        this.typesearchcurent = typesearchcurent;
    }
          public void chooseTabll() {
        try {
            setTabString("TABLL");
            SessionProvider.setSessionMapValue("TABLL1",tabString);
      //      listSelected.clear();
//            if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
//                 SessionProvider.setSessionMapValue("listSelectedEx",null);
//             }
//             if(SessionProvider.getSessionMapValue("listN100") != null){
//                 SessionProvider.setSessionMapValue("listN100", null);
//             }

        } catch (Exception e) {
        }
    }


    public void chooseTabCM() {
        try {
            setTabString("TABCM");
            SessionProvider.setSessionMapValue("TABLL1", tabString);
//            listN500DB  = (List<N500>) SessionProvider.getSessionMapValue("listN500");
//            setTabString("TABCN");
            //listSelected.clear();
            if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                SessionProvider.setSessionMapValue("listSelectedEx", null);
            }
            if (SessionProvider.getSessionMapValue("listN100") != null) {
                SessionProvider.setSessionMapValue("listN100", null);
            }
        } catch (Exception e) {
        }
    }

    public void chooseTabTK() {
        try {
            setTabString("TABTK");
            SessionProvider.setSessionMapValue("TABLL1", tabString);
        } catch (Exception e) {
        }
    }

    public void chooseTabTL() {
        try {
            setTabString("TABTL");
            SessionProvider.setSessionMapValue("TABLL1", tabString);
        } catch (Exception e) {
        }
    }

    public void chooseTabLSKH() {
        try {
            setTabString("TABLSKH");
            SessionProvider.setSessionMapValue("TABLL1", tabString);
        } catch (Exception e) {
        }
    }

    public void chooseTabLST() {
        try {
            setTabString("TABLST");
            SessionProvider.setSessionMapValue("TABLL1", tabString);
        } catch (Exception e) {
        }
    }

    public void chooseTabLSCN() {
        try {
            setTabString("TABLSCN");
            SessionProvider.setSessionMapValue("TABLL1", tabString);
        } catch (Exception e) {
        }
    }
    
      public void chooseTabdv() {
        try {
            setTabString("TABDV");
            SessionProvider.setSessionMapValue("TABLL1", tabString);
        } catch (Exception e) {
        }
    }

    /**
     * @return the tabString
     */
    public String getTabString() {
           if (SessionProvider.getSessionMapValue("TABLL1") != null) {
            tabString = SessionProvider.getSessionMapValue("TABLL1").toString();
        } else {
        }
        return tabString;
    }

    /**
     * @param tabString the tabString to set
     */
    public void setTabString(String tabString) {
        this.tabString = tabString;
    }


}
